package cn.itcast.bos.service.take_delivery.impl;

import cn.itcast.bos.index.WayBillIndexRepository;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.take_delivery.WayBillRepository;
import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.service.take_delivery.WayBillService;

import java.util.List;

@Service
@Transactional
public class WayBillServiceImpl implements WayBillService {
	@Autowired
	private WayBillRepository wayBillRepository;

	@Autowired
	private WayBillIndexRepository wayBillIndexRepositorytory;

	@Override
	public void save(WayBill model) {

		if (model.getId()==null){
			model.setSignStatus(1);//待发送
			wayBillRepository.save(model);
			wayBillIndexRepositorytory.save(model);
		}else{
			if (model.getSignStatus()==1){
				wayBillIndexRepositorytory.save(model);
				wayBillRepository.save(model);
			}else{
				//运单已经派发中,不能修改
				throw  new RuntimeException("运单已经发出,不能进行修改");
			}


		}

		
	}
	@Override
	public Page<WayBill> findPageData(WayBill model, Pageable pageable) {
		//无条件查询
		if (StringUtils.isBlank(model.getWayBillNum())
				&& StringUtils.isBlank(model.getSendAddress())
				&& StringUtils.isBlank(model.getRecAddress())
				&& StringUtils.isBlank(model.getSendProNum())
				&& (model.getSignStatus() == null || model.getSignStatus() == 0)) {
			// 无条件查询 、查询数据库
			return wayBillRepository.findAll(pageable);
		} else {
				BoolQueryBuilder query = new BoolQueryBuilder(); //布尔查询,多条件组合查询
				if (StringUtils.isNoneBlank(model.getWayBillNum())) {
					QueryBuilder tempQuery =
							new TermQueryBuilder("wayBillNum", model.getWayBillNum());
					query.must(tempQuery);
				}
				if (StringUtils.isNoneBlank(model.getSendAddress())) {

					QueryBuilder wildcardQuery =
							new WildcardQueryBuilder("sendAddress", "*" + model.getSendAddress() + "*");

					QueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder(model.getSendAddress()).
							field("sendAddress").
							defaultOperator(QueryStringQueryBuilder.Operator.AND);

					BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
					boolQueryBuilder.should(wildcardQuery);
					boolQueryBuilder.should(queryStringQueryBuilder);
					query.must(boolQueryBuilder);
				}
				if (StringUtils.isNoneBlank(model.getRecAddress())) {
					QueryBuilder wildcardQuery =
							new WildcardQueryBuilder("recAddress", "*" + model.getRecAddress() + "*");
					QueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder(model.getRecAddress()).
							field("recAddress").
							defaultOperator(QueryStringQueryBuilder.Operator.AND);

					BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
					boolQueryBuilder.should(wildcardQuery);
					boolQueryBuilder.should(queryStringQueryBuilder);
					query.must(boolQueryBuilder);
				}
				if (StringUtils.isNoneBlank(model.getSendProNum())) {
					QueryBuilder tempQuery =
							new TermQueryBuilder("sendProNum", model.getSendProNum());
					query.must(tempQuery);
				}
				if (model.getSignStatus() != 0 && model.getSignStatus() != null) {
					QueryBuilder tempQuery =
							new TermQueryBuilder("signStatus", model.getSignStatus());
					query.must(tempQuery);
				}
				SearchQuery searchQuery = new NativeSearchQuery(query);
				searchQuery.setPageable(pageable);
				return wayBillIndexRepositorytory.search(searchQuery);
			}


	}
	@Override
	public WayBill findByWaybillNum(String wayBillNum) {
		
		return wayBillRepository.findByWayBillNum(wayBillNum);
	}
	//同步索引库
	@Override
	public void syncIndex() {
		List<WayBill> wayBills = wayBillRepository.findAll();
		wayBillIndexRepositorytory.save(wayBills);
	}


	
}
